$(function(){
   $("#evalName").hide();//隐藏
   
   $("#nu").click(function(){
	   $("#evalName").show();
   });
   $("#su").click(function(){
	   $("#evalName").hide();
   });
   
   $("#evalName2").hide();//隐藏
   
   $("#nu2").click(function(){
	   $("#evalName2").show();
   });
   $("#su2").click(function(){
	   $("#evalName2").hide();
   });
   
   /*$.post("expertEvalList",function(data){
	   var expertEvalList = data.data;
	   for(var i in expertEvalList){
		   $("#cname").innerHTML(expertEvalList[i].user_name);
		   var aa = expertEvalList[i].user_name;
		   if(expertEvalList[i].user_name == null){
			   $("#cname").innerHTML(expertEvalList[i].eval_name);
		   }
	   }
   },"json");*/
});


function operating_status(value, row, index) {
/*	return [
		"<span>处理</span>"
	].join('');	*/
	
//  return "<span>处理</span>";
	 row;
	 if(row.user_name == null){
		  return [
			"<span>"+row.eval_name+"</span>"
		 ].join('');
	 }
	 if(row.user_name != null){
		  return [
			"<span>"+row.user_name+"</span>"
		 ].join('');
	 }
}


//添加评价信息

// 校验弹框输入数据
$("#addForm").validate({
	errorElement : 'span',
	errorPlacement : function(error, element) {
		error.appendTo(element.parent());
	},
	rules : {
		shopId : "required",
		evalContent : "required",
	},
	messages : {
		shopId : "请选择艺术品",
		evalContent : "请输入评价内容",
	},
	submitHandler : function(form) {
		$('#saveBtn').attr("disabled",true);
	    setTimeout("$('#saveBtn').removeAttr('disabled')",3000);
		var url = "addExpertEval";
		var shopId = $("#shopId").val();
		var evalContent = $("#evalContent").val();
		var su = $('input:radio[name="su"]:checked').val();
		var	evalUserId = null;
		var evalName = null;
		if( su == 0){
			evalUserId = $("#userId").val();
		}else{
			evalName = $("#evalName").val();
		}
		
		$.post(url,{shopId:shopId,evalContent:evalContent,evalUserId:evalUserId,evalName:evalName}, function(data){
			if(data.code == 200){
				layer.msg(data.msg, {icon : 1,time : 1500,});
				setTimeout(function(){
					$("#modal-add").modal('hide');
					$('#table').bootstrapTable("refresh");
				},2000);
			}
		},"json");
	},
});



//修改 查看
$("#edit").click(function() {
	var ids = getSelections(); 
	var expertEval = getSelectionss(); 
	if (ids.length == 1) {
		$("input[name=id]").val(expertEval[0].id);
		$("select[name=shopId2]").val(expertEval[0].shop_id);
		$("textarea[name=evalContent2]").val(expertEval[0].eval_content);
		if(expertEval[0].eval_user_id == $("#userId").val()){
			$("#su2").attr("checked",true);
		}else{
			$("#nu2").attr("checked",true);
			  $("#evalName2").show();
			$("input[name=evalName2]").val(expertEval[0].eval_name);
		}

		$('#modal-edit').modal('toggle');

	}else if(ids.length == 0){
		layer.msg("未选择数据");
	}else{
		layer.msg("请勿勾选多个");
	}
});

//修改
$("#editBtn").click(function(){
	var id = $("#id").val();
	var shopId = $("#shopId2").val();
	var evalContent = $("#evalContent2").val();
	var evalUserId = null;
	var evalName = null;
	var su = $('input:radio[name="su2"]:checked').val();
	if(su == 0){
		evalUserId = $("#userId2").val();
	}else{
		evalName = $("#evalName2").val();
	}
	
	
	$.post("updateExpertEval",{id:id,shopId:shopId,evalContent:evalContent,evalName:evalName,evalUserId:evalUserId},function(data){
		if(data.code == 200){
			layer.msg(data.msg, {icon : 1,time : 1500});
			setTimeout(function(){
				$("#modal-edit").modal('hide');
				$('#table').bootstrapTable("refresh");
			},2000);
		}
	},"json")
});



// 批量删除
$("#deleteBtn").click(function() {
	var ids = getSelections();
	if (ids == "") {
		layer.msg("未选择数据");
	} else {
		layer.confirm('确定要删除所选数据吗？',{btn: ['确定','取消']},function (){
			$.post("deleteExpertEval", {
				pIds : JSON.stringify(ids)
			}, function(data) {
				if(data.code == 200){
					layer.msg(data.msg, {icon : 1,time : 1500});
				}else{
					layer.msg(data.msg, {icon : 2,time : 1500});
				}
				$('#table').bootstrapTable('remove', {
					field : 'id',
					values : ids,
				});
			}, "json");
			return true;
		});
	}
});



function getSelections() {
	return $.map($('#table').bootstrapTable('getSelections'),function(row) {
		return row.id;
	});
}

function getSelectionss() {
	return $.map($('#table').bootstrapTable('getSelections'),function(row) {
		return row;
	});
}

